package com.thz.websqlit.common.util;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.Random;
import java.util.UUID;

public final class DataUtil {

    /**
     * 随机产生的字符串
     */
    private final static String RAND_STRING = "123456789";

    /**
     * 产生随机数
     */
    private final static Random RANDOM = new Random();
    /**
     * 获取随机的验证码
     * @param number  验证码的位数
     * @return
     */
    public static String getVfcCode(Integer number)
    {

        String randomString="";
        int randomIndex;
        char charAt;
        //根据字符串的长度产生随机下标
        for(int i = 0; i < number; i++){
            randomIndex = RANDOM.nextInt(RAND_STRING.length());
            charAt = RAND_STRING.charAt(randomIndex);
            randomString += String.valueOf(charAt);
        }
        return randomString;
    }

    /**
     * 获取UUID
     * @return
     */
    public static String getUUID(){
        return  UUID.randomUUID().toString();
    }

    /**
     * 获取汉字首字母
     * @param src
     * @return
     */
    public static String getChCharInit(String src){
        String rst = "#";
        if(src == null  || src.length() == 0){
            return rst;
        }
        char word = src.charAt(0);
        String[] pinyin = PinyinHelper.toGwoyeuRomatzyhStringArray(word);
        if(pinyin == null || pinyin.length == 0){
            return rst;
        }
        String startString = pinyin[0];
        if(startString == null || startString.length() == 0){
            return rst;
        }

        return startString.substring(0,1).toUpperCase();
    }
}
